import { request, checkPhone, checkEmptyStr, WXshowToast } from '../../utils/index.js';
import { Api } from '../../config/api.js';

import regeneratorRuntime from '../../lib/runtime/runtime'; //es7
Page({
  data: {
    disabled: false,
    auth: "获取验证码",
    formData: {
      phone: '',
      password: '',
      code: '',
      // V8YMJY
      yqm: '',
      // yqm: 'YX86',
      openid:""
    }
  },

countdown:60,
onLoad(options){
  console.log(options)
  if(options){
    console.log(options.yqm)
    let yqm1 = 'formData.yqm'
    this.setData({
      [yqm1]:options.yqm
    })
  }
  console.log(this.data)
},
onShow:function(){
    this.Openid()
},



  handleInput(e) {//公共
    let value = e.currentTarget.dataset.name == 'yqm'||e.currentTarget.dataset.name =='password'?e.detail.value:this.validateNumber(e.detail.value)
    const { formData } = this.data;
    this.setData({
      formData: {
        ...formData,
         [e.currentTarget.dataset.name]: value
      }
    });
  },
  validateNumber(val) {
    return val.replace(/\D/g, '')
  },
navlogin(){
  let that = this
  wx.setStorageSync('login', true)
  wx.redirectTo({
    url: '/pages/login/login',
  })
},
//密码
    handleInputpass(e) { 
    let value = e.detail.value
    const { formData } = this.data;
    this.setData({
        formData: {
        ...formData,
        [e.currentTarget.dataset.name]: value
        }
    });
    },

//发送验证码
  getMsgCode() {
    
    const { phone } = this.data.formData;
    const isValid = checkPhone(phone);

    if (!isValid) {
      WXshowToast('请输入正确手机号');
      return;
    }

    // 倒计时
   this.setData({
        disabled: true
   })

 var tiem = setInterval(() => {
     if (this.countdown == 0) {
         this.setData({
             auth: "获取验证码",
             disabled: false
         })
         clearInterval(tiem)
         this.countdown=60
     } else {
        this.countdown--
         this.setData({
             disabled: true,
             auth: `${this.countdown}后重新获取`,
         })
     }

 }, 1000)

    request({
      url: Api.getMsgCode,
      data: { phone },
      method: 'POST'
    })
      .then(res => {
        // WXshowToast(res.data.data.msg);
      })
      .catch(e => {WXshowToast(e.data.msg);});
  },


// 获取openid
async Openid(){
     wx.login({
         success: async (res) => {
             const code = res.code
             const {data}  = await  request({
                url: Api.getOpenid,
                data:{
                    code
                },
                method:"POST"
            })
            const { formData } = this.data;
              this.setData({
                  formData: {
                      ...formData,
                      openid: data.data.openid
                  }
              })
         }, fail:(err)=>{
            
         }
     })

},


  async getwechatuserinfo(){
    let that = this
    console.log(12)
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        this.setData({
          wxuserInfo: res.userInfo//获取微信用户个人信息
        })
        this.register()
      },
      fail(){
          
      }
    })
  },
  // 注册
  async register() {
    let that = this
    const { formData } = this.data;
    const isValid = checkPhone(formData.phone);
    this.data.formData.face = this.data.wxuserInfo.avatarUrl
    console.log(this.data.formData.avatarUrl)
    if (!isValid ) {
      WXshowToast('手机号填写错误');
      return;
    }
    if(!formData.code){
          WXshowToast('请填写验证码');
          return;
    }
    if(!formData.password){
         WXshowToast('密码不能为空');
         return;
    }
    


     let data=await request({
       url:Api.register,
       data:formData,
       method: 'POST'
     })
    if(data.data.code ==1001){
        console.log(1)
        WXshowToast(data.data.msg);
    }else{
      let {jwt_token,openid} = data.data.data
      console.log('注册成功',data.data)
      console.log('jwt_token',jwt_token)
      console.log('openid',openid)
      wx.setStorageSync('token', jwt_token);
      wx.setStorageSync('userInfo', data.data.data);
// console.log('dormdata',that.data.formData)
      wx.setStorageSync("openid", that.data.formData.openid)
        wx.redirectTo({
            url: '/pages/index/index'
        });
    }


  },

 
});
